<template>
    <div class="container-background">
        <div class="container right-panel-active">
            <!-- Sign Up -->
            <div class="container__form container--signup">
                <div class="login-switch" @click="loginModeQr = !loginModeQr"
                    style="position: absolute; right: 10px; top: 10px; font-size: 28px; cursor: pointer;">
                    <a-tooltip content="扫码登录" position="left" background-color="#3491FA" v-if="!loginModeQr">
                        <svg-icon icon-class="switch-qr" />
                    </a-tooltip>
                    <a-tooltip content="账号登录" position="left" background-color="#3491FA" v-else>
                        <svg-icon icon-class="switch-account" />
                    </a-tooltip>
                </div>
                <a-form :model="loginForm" ref="loginFormRef" :style="{ width: '290px' }" layout="vertical"
                    class="form">
                    <template v-if="loginModeQr">
                        <div>扫码登录</div>
                    </template>
                    <a-row :gutter="15" v-else>
                        <a-col :span="24">
                            <a-form-item field="username">
                                <a-input v-model="loginForm.username" placeholder="请输入账号"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item field="password">
                                <a-input-password v-model="loginForm.password" placeholder="请输入密码"></a-input-password>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item field="code">
                                <a-input v-model="loginForm.code" placeholder="请输入验证码"
                                    :style="{ width: '63%' }"></a-input>
                                <div class="register-code">
                                    <img class="register-code-img" :src="codeUrl" @click="getCode()" />
                                </div>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-button size="large" class="btn" type="primary" @click="login">登 录</a-button>
                        </a-col>
                    </a-row>
                </a-form>
            </div>

            <!-- Sign In -->
            <div class="container__form container--signin">
                <a-form :model="registerForm" ref="registerForm" :style="{ width: '290px' }" layout="vertical"
                    class="form">
                    <a-row :gutter="15">
                        <a-col :span="24">
                            <a-form-item field="username">
                                <a-input v-model="registerForm.username" placeholder="请输入账号"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item field="password">
                                <a-input-password v-model="registerForm.password"
                                    placeholder="请输入密码"></a-input-password>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item field="confirmPassword">
                                <a-input-password v-model="registerForm.confirmPassword"
                                    placeholder="请再次输入密码"></a-input-password>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item field="code">
                                <a-input v-model="registerForm.code" placeholder="请输入验证码"
                                    :style="{ width: '63%' }"></a-input>
                                <div class="register-code">
                                    <img :src="codeUrl" @click="getCode" class="register-code-img" />
                                </div>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-button size="large" class="btn" type="primary" @click="register">注 册</a-button>
                        </a-col>
                    </a-row>
                </a-form>
            </div>

            <!-- Overlay -->
            <div class="container__overlay">
                <div class="overlay">
                    <div class="overlay__panel overlay--left">
                        <button class="btn" id="signIn" @click="signIn">注 册</button>
                    </div>
                    <div class="overlay__panel overlay--right">
                        <button class="btn" id="signUp" @click="signUp">登 录</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

</template>

<script setup>
import { ref, reactive, watch } from 'vue';
import { getCodeImg, register } from "@/api/login";
import { useRouter, useRoute } from 'vue-router';
import useUserStore from '@/stores/user';

const userStore = useUserStore();
const router = useRouter();
const route = useRoute();
const redirect = ref(undefined)

watch(route, (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect
}, { immediate: true })

// 登录注册切换样式
function signIn() {
    const container = document.querySelector(".container");
    container.classList.remove("right-panel-active");
}
function signUp() {
    const container = document.querySelector(".container");
    container.classList.add("right-panel-active");
}
// 注册码
const codeUrl = ref("");
const captchaEnabled = ref(false);
function getCode() {
    getCodeImg().then(({ data }) => {
        captchaEnabled.value = data.captchaEnabled === undefined ? true : data.captchaEnabled;
        codeUrl.value = `data:image/gif;base64,${data.img}`;
        loginForm.value.uuid = data.uuid;
        registerForm.value.uuid = data.uuid;
    })
}
getCode();

// 注册
const registerForm = ref({
    username: '',
    password: '',
    confirmPassword: '',
    uuid: '',
    code: ''
})

// 登录
const loginModeQr = ref(false)
const loginFormRef = ref(null)
const loginForm = ref({
    username: '',
    password: '',
    uuid: '',
    code: ''
})

function login() {
    // loginFormRef.validate().then(() => {
    userStore.login(loginForm.value).then(() => {
        const query = route.query;
        const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
            if (cur !== "redirect") {
                acc[cur] = query[cur];
            }
            return acc;
        }, {});
        router.push({ path: redirect.value || "/", query: otherQueryParams });
    })
    // }).catch(err => {
    //     if (captchaEnabled.value) {
    //         getCode()
    //     }
    // })
}
</script>


<style scoped>
.register-code {
    width: 33%;
    height: 40px;
    float: right;

    img {
        height: 100%;
        padding-left: 12px;
        cursor: pointer;
        vertical-align: middle;
    }
}

button,
hr,
input {
    overflow: visible;
}

audio,
canvas,
progress,
video {
    display: inline-block;
}

progress,
sub,
sup {
    vertical-align: baseline;
}

html {
    font-family: sans-serif;
    line-height: 1.15;
    -ms-text-size-adjust: 100%;
    -webkit-text-size-adjust: 100%;
}

.container-background {
    margin: 0;
}

menu,
article,
aside,
details,
footer,
header,
nav,
section {
    display: block;
}

h1 {
    font-size: 2em;
    margin: 0.67em 0;
}

figcaption,
figure,
main {
    display: block;
}

figure {
    margin: 1em 40px;
}

hr {
    box-sizing: content-box;
    height: 0;
}

code,
kbd,
pre,
samp {
    font-family: monospace, monospace;
    font-size: 1em;
}

a {
    background-color: transparent;
    -webkit-text-decoration-skip: objects;
}

a:active,
a:hover {
    outline-width: 0;
}

abbr[title] {
    border-bottom: none;
    text-decoration: underline;
    text-decoration: underline dotted;
}

b,
strong {
    font-weight: bolder;
}

dfn {
    font-style: italic;
}

mark {
    background-color: #ff0;
    color: #000;
}

small {
    font-size: 80%;
}

sub,
sup {
    font-size: 75%;
    line-height: 0;
    position: relative;
}

sub {
    bottom: -0.25em;
}

sup {
    top: -0.5em;
}

audio:not([controls]) {
    display: none;
    height: 0;
}

img {
    border-style: none;
}

svg:not(:root) {
    overflow: hidden;
}

button,
input,
optgroup,
select,
textarea {
    font-family: sans-serif;
    font-size: 100%;
    line-height: 1.15;
    margin: 0;
}

button,
select {
    text-transform: none;
}


[type="button"]::-moz-focus-inner,
[type="reset"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner,
button::-moz-focus-inner {
    border-style: none;
    padding: 0;
}

[type="button"]:-moz-focusring,
[type="reset"]:-moz-focusring,
[type="submit"]:-moz-focusring,
button:-moz-focusring {
    outline: ButtonText dotted 1px;
}

fieldset {
    border: 1px solid silver;
    margin: 0 2px;
    padding: 0.35em 0.625em 0.75em;
}

legend {
    box-sizing: border-box;
    color: inherit;
    display: table;
    max-width: 100%;
    padding: 0;
    white-space: normal;
}


textarea {
    overflow: auto;
}

[type="checkbox"],
[type="radio"] {
    box-sizing: border-box;
    padding: 0;
}

[type="number"]::-webkit-inner-spin-button,
[type="number"]::-webkit-outer-spin-button {
    height: auto;
}


[type="search"]::-webkit-search-cancel-button,
[type="search"]::-webkit-search-decoration {
    -webkit-appearance: none;
}

::-webkit-file-upload-button {
    -webkit-appearance: button;
    font: inherit;
}

summary {
    display: list-item;
}

[hidden],
template {
    display: none;
}

:root {
    /* COLORS */
    --white: #e9e9e9;
    --gray: #333;
    --blue: #0367a6;
    --lightblue: #008997;

    /* RADII */
    --button-radius: 0.7rem;

    /* SIZES */
    --max-width: 758px;
    --max-height: 420px;

    font-size: 16px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}

.container-background {
    align-items: center;
    background-color: #e9e9e9;
    background: url("@/assets/login/background.jpg");
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    display: grid;
    height: 100vh;
    place-items: center;
}

.form__title {
    font-weight: 300;
    margin: 0;
    margin-bottom: 1.25rem;
}

.link {
    color: #333;
    font-size: 0.9rem;
    margin: 1.5rem 0;
    text-decoration: none;
}

.container {
    background-color: #e9e9e9;
    border-radius: 0.7rem;
    box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25), 0 0.7rem 0.7rem rgba(0, 0, 0, 0.22);
    height: 420px;
    max-width: 758px;
    overflow: hidden;
    position: relative;
    width: 100%;
}

.container__form {
    height: 100%;
    position: absolute;
    top: 0;
    transition: all 0.6s ease-in-out;
}

.container--signin {
    left: 0;
    width: 50%;
    z-index: 2;
}

.container.right-panel-active .container--signin {
    transform: translateX(100%);
}

.container--signup {
    left: 0;
    opacity: 0;
    width: 50%;
    z-index: 1;
}

.container.right-panel-active .container--signup {
    -webkit-animation: show 0.6s;
    animation: show 0.6s;
    opacity: 1;
    transform: translateX(100%);
    z-index: 5;
}

.container__overlay {
    height: 100%;
    left: 50%;
    overflow: hidden;
    position: absolute;
    top: 0;
    transition: transform 0.6s ease-in-out;
    width: 50%;
    z-index: 100;
}

.container.right-panel-active .container__overlay {
    transform: translateX(-100%);
}

.overlay {
    background-color: #008997;
    background: url("@/assets/login/background.jpg");
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    height: 100%;
    left: -100%;
    position: relative;
    transform: translateX(0);
    transition: transform 0.6s ease-in-out;
    width: 200%;
}

.container.right-panel-active .overlay {
    transform: translateX(50%);
}

.overlay__panel {
    align-items: center;
    display: flex;
    flex-direction: column;
    height: 100%;
    justify-content: center;
    position: absolute;
    text-align: center;
    top: 0;
    transform: translateX(0);
    transition: transform 0.6s ease-in-out;
    width: 50%;
}

.overlay--left {
    transform: translateX(-20%);
}

.container.right-panel-active .overlay--left {
    transform: translateX(0);
}

.overlay--right {
    right: 0;
    transform: translateX(0);
}

.container.right-panel-active .overlay--right {
    transform: translateX(20%);
}

.btn {
    background-color: #0367a6;
    background-image: linear-gradient(90deg, #0367a6 0%, #008997 74%);
    border-radius: 20px;
    border: 1px solid #0367a6;
    color: #e9e9e9;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: bold;
    letter-spacing: 0.1rem;
    padding: 0.9rem 4rem;
    text-transform: uppercase;
    transition: transform 80ms ease-in;
}

.form>.btn {
    margin-top: 1.5rem;
}

.btn:active {
    transform: scale(0.95);
}

.btn:focus {
    outline: none;
}

.form {
    background-color: #e9e9e9;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    padding: 0 3rem;
    height: 100%;
    text-align: center;
}

.input {
    background-color: #fff;
    border: none;
    padding: 0.9rem 0.9rem;
    margin: 0.5rem 0;
    width: 100%;
}

@-webkit-keyframes show {

    0%,
    49.99% {
        opacity: 0;
        z-index: 1;
    }

    50%,
    100% {
        opacity: 1;
        z-index: 5;
    }
}

@keyframes show {

    0%,
    49.99% {
        opacity: 0;
        z-index: 1;
    }

    50%,
    100% {
        opacity: 1;
        z-index: 5;
    }
}
</style>
